import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import VueCookie from 'vue-cookie'
import filters from './filters'
import VueMoment from 'vue-moment'

Vue.config.productionTip = false

Vue.use(ElementUI)
Vue.use(VueCookie)
Vue.use(VueMoment)

let userinfo = JSON.parse(VueCookie.get('userinfo'))

axios.defaults.baseURL = process.env.VUE_APP_BASE_URL
axios.defaults.headers.common['Content-Type'] = 'application/json'
if (userinfo && userinfo.username) {
  axios.defaults.headers.common['Authorization'] = userinfo.token
}
Vue.prototype.$axios = axios

// 判断是否登录
router.beforeEach((to, from, next) => {
  if ((!userinfo || !userinfo.username) && to.path !== '/login') {
    next('/login')
  }
  next()
})

// 添加过滤器
Object.keys(filters).forEach(k => Vue.filter(k, filters[k]))

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
